#include "tfviewer.h"
#include <QPainter>

TFViewer::TFViewer(QWidget *parent)
	: QWidget(parent)
{
	this->setMinimumHeight(HEIGHT);
	this->filename = "";

	for(int i = 0;i < 10;i++)
		weights[i] = 127;
}

TFViewer::~TFViewer()
{

}

void TFViewer::setTF(const string& filename)
{
	this->filename = filename;
}

void TFViewer::setWeights(const int w[10])
{
	for(int i = 0;i < 10;i++)
		weights[i] = w[i];
}

void TFViewer::paintEvent(QPaintEvent *event) 
{
	if (filename != "")
	{
		QPixmap tf(filename.c_str());

		QPainter painter(this);
		painter.drawPixmap(0, 0, this->width(), HEIGHT, tf);
		painter.setBrush(QBrush(QColor(Qt::red), Qt::SolidPattern));
		for(int i = 0;i < 9;i++)
		{
			int x1 = (int)((float)i / 9.f * (float)this->width());
			int y1 = HEIGHT - (int)((float)weights[i] / 255.f * HEIGHT);
			int x2 = (int)((float)(i + 1) / 9.f * (float)this->width());
			int y2 = HEIGHT - (int)((float)weights[i + 1] / 255.f * HEIGHT);
			painter.drawLine(x1, y1, x2, y2);
		}
		painter.end();
	}
}
